package com.huizu.common.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

public class SmsUtil {

    /**
     * 发送短信
     * @param phoneNumbers 手机号
     * @param param 参数
     */
    public static void sendMessage(String phoneNumbers,String param){
        //TODO 改成自己的
        DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", "LTAI5tQgdkZJuhef5DeYMixb", "2IBEQaWenkzC8iH9n2AI2I6yUHjTFZ");
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSysRegionId("cn-qingdao");
        request.setPhoneNumbers(phoneNumbers);
        request.setSignName("山东风正信息科技");
        request.setTemplateCode("您的验证码为：${code}，请勿泄露于他人！");
        request.setTemplateParam("{\"code\":\""+param+"\"}");
        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println("短信发送成功");
        }catch (ClientException e) {
            e.printStackTrace();
        }
    }

    // 生成随机数验证码
    public static String generateVerificationCode() {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            sb.append(random.nextInt(10));
        }
        return sb.toString();
    }


}
